// 本地存储工具函数

/**
 * 存储数据到本地
 */
export const setStorage = (key: string, data: any) => {
  try {
    uni.setStorageSync(key, data);
    return true;
  } catch (error) {
    console.error('存储数据失败:', key, error);
    return false;
  }
};

/**
 * 从本地获取数据
 */
export const getStorage = <T = any>(key: string, defaultValue?: T): T | null => {
  try {
    const data = uni.getStorageSync(key);
    return data !== '' ? data : defaultValue || null;
  } catch (error) {
    console.error('获取存储数据失败:', key, error);
    return defaultValue || null;
  }
};

/**
 * 删除本地存储数据
 */
export const removeStorage = (key: string) => {
  try {
    uni.removeStorageSync(key);
    return true;
  } catch (error) {
    console.error('删除存储数据失败:', key, error);
    return false;
  }
};

/**
 * 清空所有本地存储
 */
export const clearStorage = () => {
  try {
    uni.clearStorageSync();
    return true;
  } catch (error) {
    console.error('清空存储失败:', error);
    return false;
  }
};

/**
 * 获取存储信息
 */
export const getStorageInfo = () => {
  try {
    return uni.getStorageInfoSync();
  } catch (error) {
    console.error('获取存储信息失败:', error);
    return null;
  }
};

export const clearIMProfile = () => {
  localStorage.removeItem('IM_TOKEN');
  localStorage.removeItem('IM_CHAT_TOKEN');
  localStorage.removeItem('IM_USERID');
};

export const getWsUrl = () => localStorage.getItem('wsUrl') || process.env.WS_URL!;
export const getApiUrl = () => localStorage.getItem('apiUrl') || process.env.API_URL!;
export const getChatUrl = () => localStorage.getItem('chatUrl') || process.env.CHAT_URL!;
